package com.lq.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
 * map集合练习
 * @author liuqing
 * @since 2022/2/17
 */
public class MapDemo {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        // 添加元素
        map.put("A", 90);
        map.put("B", 80);
        map.put("C", 70);
        map.put("D", 60);
        map.put("E", 50);
        map.put("F", 40);

        // 修改元素
        map.replace("F", 100);

        // 删除元素
        map.remove("C");

        //读取
        System.out.println(map.get("A"));

        // map遍历5种方式 1.使用For-Each迭代entries
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println("key" + entry.getKey() + "value" + entry.getValue());
        }

        System.out.println("===================================");

        // 2.使用For-Each迭代keys和values
        for (String key : map.keySet()) {
            System.out.println("key=" + key);
        }
        for (Integer value : map.values()) {
            System.out.println("value" + value);
        }
        System.out.println("===================================");
        // 3.使用Iterator
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> next = iterator.next();
            System.out.println("key=" + next.getKey() + " " + "value=" + next.getValue());
        }
        System.out.println("===================================");
        // 4.迭代key并搜索values
        for (String key : map.keySet()) {
            Integer value = map.get(key);
            System.out.println("key=" + key + "值=" + value);
        }

        System.out.println("===================================");
        // 5.Lambda
        map.forEach((key, value) -> {
            System.out.println(key + ":" + value);
        });


    }
}
